<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/1
 * Time: 上午9:09
 */
class Logreg extends MY_Controller
{
	public function index()
	{
		$post_data = $this->input->post();
        if($post_data){
            $has_error = 0;
            if(empty($post_data['phone'])){
                $this->smarty->assign("phone_err", 1);
                $has_error = 1;
            }else{

                header("Location:/logreg/next/{$post_data['phone']}?openid={$post_data['open_id']}");exit;
            }
		}else{
			$open_id = $this->input->get("openid");
		
		// print_r($open_id);exit;

			if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && empty($open_id)) {
                $wx_config = get_field_by_where("o2o_school_wx_config", "app_id, app_secret", "school_id='{$this->school_id}'");
                if(empty($wx_config['app_id']) || empty($wx_config['app_secret'])){
                    
                }else{
                    header("Location:/login_wx");exit;
                }
				//微信浏览器
				//微信登录处理
			}


			$this->smarty->assign("open_id", $open_id);
		}
		$this->smarty->display("logreg.html");
	}

	public function next($phone)
	{
		$open_id = $this->input->get("openid");
		// print_r($open_id);
		$this->smarty->assign("open_id", $open_id);
		$this->smarty->assign("phone", $phone);
		$this->load->model('User_model');
		$user = $this->User_model->get_user_by_phone($phone);
		if($user){
			//用户存在
			$this->smarty->assign("phone_has", 1);
		}else{
			//用户不存在
			$this->smarty->assign("phone_has", 0);
		}

		$post_data = $this->input->post();
        if($post_data){
        	$has_error = 0;
        	if(empty($post_data['password'])){
                $this->smarty->assign("password_err", 1);

                $has_error = 1;
            }

            if(empty($user)){

            	//用户不存在，验证验证码
            	if(empty($post_data['vcode'])){
	                $this->smarty->assign("vcode_err", 1);
	                $has_error = 1;
            	}

	            //判断验证码是否正确
	            if(empty($phone)==false && empty($post_data['vcode'])==false){
	                //查询用户最后一条code
	                $this->load->model("Verification_code");
	                $code_model = $this->Verification_code->getLastCodeByPhone($phone,1);
	                if($code_model['is_use']==1||$code_model['code']!=$post_data['vcode']){
	                    //验证码错误
	                    $this->smarty->assign("vcode_err", 1);
	                    $has_error = 1;
	                }
	            }
            }else{
            	//用户存在，判断登录密码
            	$password = md5(md5($post_data['password']) . $user['rand_str']);
            	
                if($user['password']!=$password){
                	$this->smarty->assign("password_err", 1);
                   	$has_error = 1;
                }
            }

            if($has_error == 0){

            	if(empty($user)){
            		$md5_pass = md5($post_data['password']);
					$rand_str = '';
					for($i=0; $i<10; $i++){
						$rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
					}

					$password = md5($md5_pass.$rand_str);
					$register_ip = $_SERVER["REMOTE_ADDR"];

	                //添加注册
	                $this->User_model->reg_user($phone,$rand_str,$password,$register_ip,$open_id);
	                $user = $this->User_model->get_user_by_phone($phone);
            	}else{
            		if($open_id){
            			//更新openID
            			$this->User_model->update_openid($user['id'],$open_id);
            		}
            	}

        		//设置cookie
                $user_data = array(
                    'id' => $user['id'],
                    'head_image' => $user['head_image_url'],
                    'user_name' => $user['login_name'],
                    'mobile' => $user['mobile'],
                    'logged_in' => 1);
                $this->session->set_userdata('user_data', $user_data);
                $curr_url =  $this->session->userdata('curr_url');
                if(empty($curr_url)){
                    show_message("/login", "注册成功");exit;
                }else{
                    show_message($curr_url, "注册成功");exit;
                }

            }

        	
        }

		
		$this->smarty->display("logreg_next.html");
	}

	function logout(){

      $this->session->sess_destroy();
        unset(
            $_SESSION['id'],
            $_SESSION['head_image'],
            $_SESSION['user_name'],
            $_SESSION['mobile'],
            $_SESSION['logged_in']
        );
        $curr_url =  $this->session->userdata('curr_url');
        foreach($_COOKIE as $key=>$val){
            setcookie($key, "", time() - 1, '/');
        }
        header("Location:/");exit;
    }

}